package com.xingda.packing.dmds.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingda.packing.dmds.common.DateUtils;
import com.xingda.packing.dmds.config.appset.AppSetParam;
import com.xingda.packing.dmds.enums.AlarmMsgType;
import com.xingda.packing.dmds.minitor.entity.PkAlarmType;
import com.xingda.packing.dmds.minitor.entity.PkWorker;
import com.xingda.packing.dmds.minitor.mapper.PkWorkerMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component("dtsAlarmDirectDMDSPublisher")
@ConditionalOnProperty(prefix = "spring.rabbitmq.alarm-mq", value = "enabled", havingValue = "true")
public class DTSAlarmDirectPublisher {

    @Resource(name = "alarmRabbitTemplate")
    RabbitTemplate alarmTemplate;

    @Value("${spring.unit:DTS成品捻股装箱}")
    String unit;

    @Value("${spring.rabbitmq.alarm-mq.routingKey:weixin}")
    String routingKey;

    @Value("${spring.rabbitmq.alarm-mq.agentid:1000061}")
    String agentid;

    @Resource
    PkWorkerMapper pkWorkerMapper;

    /**
     * 发送报警消息
     */
    public void sendAllWorkerAlarm(PkAlarmType pkAlarmType, String detailContent, int area) {
        sendAllWorker(pkAlarmType, AlarmMsgType.ALARM, detailContent, area);
    }

    /**
     * 发送通知消息,报警取消后给的通知
     */
    public void sendAllWorkerNotice(PkAlarmType pkAlarmType, String detailContent, int area) {
        sendAllWorker(pkAlarmType, AlarmMsgType.NOTICE, detailContent, area);
    }

    public void sendAllWorker(PkAlarmType pkAlarmType, AlarmMsgType alarmMsgType, String detailContent, int area) {
        //配置不发送
        if (!AppSetParam.is_alarm_send) {
            log.error("消息推送配置为-AppSetParam.is_alarm_send：{}", false);
            return;
        }
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        LambdaQueryWrapper<PkWorker> pkWorkerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        int i = gregorianCalendar.get(Calendar.HOUR_OF_DAY);
        log.info("当前班次:{}", i);
        if (i < 8) {
            pkWorkerLambdaQueryWrapper.in(PkWorker::getClazz, 0, 1);
        } else if (i > 8 && i < 16) {
            pkWorkerLambdaQueryWrapper.in(PkWorker::getClazz, 0, 2);
        } else {
            pkWorkerLambdaQueryWrapper.in(PkWorker::getClazz, 0, 3);
        }
        List<PkWorker> pkWorkerList = pkWorkerMapper.selectList(pkWorkerLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(pkWorkerList)) {
            log.error("未找到消息接收人，消息为-{}", pkAlarmType.getContent());
            return;
        }
        List<String> numberList = pkWorkerList.stream().map(PkWorker::getNumber).collect(Collectors.toList());
        String user_id = org.apache.commons.lang3.StringUtils.join(numberList, "|");
        String msgBuilder = alarmMsgType.getDesc() + "内容：" + pkAlarmType.getContent() + "；" + (StringUtils.hasText(detailContent) ? detailContent : "") + "；" + alarmMsgType.getResult() + "；" + alarmMsgType.getDesc() + "时间：" +
                DateUtils.getNowDate() + "；" + "所属车间：" + unit + "。";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content", msgBuilder);
        jsonObject.put("user_id", user_id);
        jsonObject.put("agentid", agentid);
        log.info("{}消息-->>>>{}", alarmMsgType.getDesc(), jsonObject);
        alarmTemplate.convertAndSend("", routingKey, jsonObject.toString());
        log.info(alarmMsgType.getDesc() + "消息发送成功" + "区域" + area);
    }
}
